package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.ResultVo;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-12-16
 */
@RestController
@CrossOrigin
@RequestMapping("/educenter/member")
public class UcenterMemberController {

    @Autowired
    UcenterMemberService ucenterMemberService;

    //登录
    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public ResultVo loginUser(@RequestBody UcenterMember ucenterMember) {
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token = ucenterMemberService.login(ucenterMember);
        return ResultVo.ok().data("token", token);
    }


    //注册
    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public ResultVo register(@RequestBody RegisterVo registerVo) {
        ucenterMemberService.register(registerVo);
        return ResultVo.ok();
    }
    //根据token获取登录信息
    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("getLoginInfo")
    public ResultVo getLoginInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = ucenterMemberService.getById(memberId);
        return ResultVo.ok().data("userInfo", member);
    }
    //根据用户id获取用户信息
    @GetMapping("getUcenter/{memberId}")
    public UcenterMember getUcenterById(@PathVariable("memberId") String memberId){
        UcenterMember member = ucenterMemberService.getById(memberId);
        return member;
    }
    //根据用户id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable("id") String memberId){
        UcenterMember member = ucenterMemberService.getById(memberId);
        //把member对象里面的值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }
    //查询某一天注册人数
    @GetMapping("countRegister/{day}")
    public ResultVo countRegister(@PathVariable String day){
        Integer count = ucenterMemberService.countRegisterDay(day);
        return ResultVo.ok().data("countRegister",count);
    }
}